//
//  ReadDataBase1.m
//  Slice
//
//  Created by lanou on 16/6/22.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "ReadDataBase1.h"
static ReadDataBase1 *manager = nil;
@implementation ReadDataBase1
+(instancetype)share {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ReadDataBase1 alloc] init];
        NSString *str = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"read.sqlite"];
        manager.db = [[FMDatabase alloc] initWithPath:str];
        
        NSLog(@"%@",str);
    });
    return manager;
}

//创建数据库以及数据库表格
- (void)creatDataBaseWithTableName:(NSString *)tableName {
        [self.db open];
    
        //   创建表格字符串
        NSString *sql = [NSString stringWithFormat:@"create table if not exists Artical%@",tableName];
        //    执行相关语句
        BOOL result = [self.db executeUpdate:sql];
        if (!result) {
            
            NSLog(@"表创建失败");
        }
        
    
    [self.db close];
}




// 添加一个模型
- (BOOL) addModel:(ListMdel *)model {
    
    [self.db open];
    NSString *sql = [NSString stringWithFormat:@"insert into Artical values (?,?)"];
    BOOL result =  [self.db executeUpdate:sql,model.title,[ListMdel achiveWithModel:model]];
    [self.db close];
    return result;
}

// 删除一个模型
- (BOOL) deleteModel:(ListMdel *)model {
    [self.db open];
    NSString *sql = [NSString stringWithFormat:@"delete from Artical where name = ?"];
    BOOL result = [self.db executeUpdate:sql,model.title];
    return result;
}

//更改某个模型
- (BOOL) updateModel:(ListMdel *)model WithNew:(ListMdel *)newModel {
    
    [self.db open];
    NSString *sql = [NSString stringWithFormat:@"update Artical set %@ = %@ where %@ = ?",model.name, model.name, model.name];
    BOOL result = [self.db executeUpdate:sql , [ListMdel  achiveWithModel:newModel]];
    [self.db close];
    return result;
}

//查询某个模型的数据
- (ListMdel *) selectWithName:(NSString *)name{
    [self.db open];
    NSString *sql = [NSString stringWithFormat:@"select * from Artical where name = ?"];
    FMResultSet *set = [self.db executeQuery:sql,name];
    NSMutableArray *marr = [NSMutableArray array];
    while (set.next) {
        NSData *data = [set dataForColumn:@"model"];
        [marr addObject:[ListMdel notAchicerWithData:data key:name]];
    }
    [self.db close];
    return marr.lastObject;
}

//查询所有模型数据
- (NSArray *)selectAllModel {
    [self.db open];
    NSString *sql = [NSString stringWithFormat:@"select * from Artical"];
    FMResultSet *set = [self.db executeQuery:sql];
    NSMutableArray *marr = [NSMutableArray array];
    
    while (set.next) {
        NSData *data = [set dataForColumn:@"model"];
        NSString *text = [set stringForColumn:@"name"];
        [marr addObject:[ListMdel notAchicerWithData:data key:text]];
    }
    return marr;
}

//查询数据库有多少张表格
- (NSInteger)howMuchTables {
    [self.db open];
    NSString *sql = @"SELECT count(*) TABLES";
    NSInteger a = 0;
 FMResultSet *set = [self.db executeQuery:sql];
    while (set.next) {
        a ++;
    }
    return a;
}






@end
